<?php
/**
 * Template Name: Options
 */
get_header(); 

require('DBHandler.php');
$dbHandler = new DBHandler;
?>

<html>
	<div id="options">
	<table>
		<tr>
			<th><b>Database Operations</b></th>
        </tr>
        <tr>
			<td>Recreate Tables</td>
			<td><a href="">Recreate Tables</a></td>
        </tr>
		<tr>
			<td>Delete Tables</td>
			<td><a href="">Delete Tables</a></td>
		</tr>
		<tr>
			<td>Status</td>
 			<td>	
				<?php
					print_r($results);
				?>
			</td>
		</tr>
	</table>

	<br>
	<h2><b>Manage Courses<b></h2>
        <h1>
            <?php
                echo $heading;
            ?>
        </h1>
        <form action="http://students.washington.edu/andreybr/wordpress/?page_id=241" method="post">
            <?php
                echo "<input type=hidden name='id' value='" . $id . "' />";
            ?>
            <table>
                <tr>
                    <td>Course Name</td>
                    <td>
                        <input name="txtName" maxlength="40"
                           <?php
                                if ($course)
                                {
                                    echo "value='" . $course->Name . "'";
                                }
                           ?> />
                    </td>
                </tr>
                <tr>
                    <td>Course Quarter</td>
                    <td>
                        <select name="selQuarter">
                            <option value="Autumn" <?php if ($course && $course->Quarter == 'Autumn') echo "selected"?>>Autumn</option>
                            <option value="Spring" <?php if ($course && $course->Quarter == 'Spring') echo "selected"?>>Spring</option>
                            <option value="Summer" <?php if ($course && $course->Quarter == 'Summer') echo "selected"?>>Summer</option>
                            <option value="Winter" <?php if ($course && $course->Quarter == 'Winter') echo "selected"?>>Winter</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>Year of Course</td>
                    <td>
                        <input name="txtYear" maxlength="4" 
                        <?php
                                if ($course)
                                {
                                    echo "value='" . $course->Year . "'";
                                }
                           ?> />
                    </td>
                </tr>
                <tr>
                    <td>Faculty</td>
                    <td>
                        <select name="selFaculty">
                            <?php
                                $rows = $dbHandler->GetAllFaculty();
                                
                                foreach($rows as $row)
                                {
                                    $output = "<option value='" . $row->Id . "' ";
                                    
                                    if ($course && $course->FacultyId == $row->Id)
                                    {
                                        $output .= " selected ";
                                    }

                                    $output .=  ">" . $row->Name . "</option>";
                                    
                                    echo $output;
                                }
                            ?>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td><input type="submit" value="Submit"/></td>
                    <td><input type="reset" value="Reset"/></td>
                </tr>
            </table>
        </form>
		<br>

	<table border="1">
		<tr>
			<th>Name</th>
			<th>Quarter</th>
			<th>Year</th>
			<th>Faculty</th>
			<th>Edit</th>
			<th>Delete</th>
		</tr>
		<?php
			$rows = $dbHandler->GetAllCourses();

			if ($rows)
			{
				foreach($rows as $row)
				{
					echo "<tr>";
					echo "<td>" . $row->Name . "</td>";
					echo "<td>" . $row->Quarter . "</td>";
					echo "<td>" . $row->Year . "</td>";
					echo "<td>" . $row->FacultyName . "</td>";
					echo "<td><a href='http://students.washington.edu/andreybr/wordpress/?page_id=241&op=edit&id=" . $row->Id . "'>Edit</a></td>";
					echo "<td><a href='http://students.washington.edu/andreybr/wordpress/?page_id=241&op=delete&id=" . $row->Id . "'>Delete</a></td>";
					echo "</tr>";
				}
			} 
			else
			{
				echo "<tr><td colspan=6>No courses found.</td></tr>";
			}
			?>
		</table>
	</div>
	<br>
</html>

